//
//  Initialize.swift
//  swiftDemo
//
//  Created by yaqiang bi on 2017/3/22.
//  Copyright © 2017年 毕小强. All rights reserved.
//
//  初始化

import UIKit

class Initialize: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        struct person {
            var earCount:Int
            init(_ earcount:Int) {
                earCount = earcount
            }
        }
        
        let p1 = person(2)
        print("这个人有\(p1.earCount)个耳朵")
        
        struct Animal{
            var category :String
            init(category ca:String) {
                category = ca
            }
        }
        let dog = Animal(category:"狗")
        print(dog.category)
        
        let color1 = Color(red: 1.0, green: 5.0, blue: 255.0)
        let color2 = Color(white: 2.0)
        let color3 = Color(3.0)
        print(color1.red)
        print(color2.red)
        print(color3.red)
        
        let questions = Questions(text: "天上有几颗星星?")
        questions.response = "无数颗"
        questions.answer()
        
        let list1 = list()
        print(list1.age)
        
        let size1 = Size(width: 2.0, height: 5.0)
        print((size1.width,size1.height))
        
        let zeroRect = rect()
        print(zeroRect)
        
        let rect1 = rect(origin: Point(x:2.0,y:3.0), size: Size(width: 100.0, height: 50.0))
        print(rect1)
        
        let rect2 = rect(center: Point(x:10.0,y:20.0), size: Size(width: 200.0, height: 300.0))
        print(rect2)
        
        let vechicel = Vehicle()
        print(vechicel.description)
        let bicycle = Bicyle()
        print(bicycle.description)
        
        let noodle1 = Noodle()
        let noodle2 = Noodle(name: "面条")
        let noodle3 = Noodle(name: "甜面条", quantity: 3)
        print("noodle1:\(noodle1.name)+\(noodle1.quantity)")
        print("noodle2:\(noodle2.name)+\(noodle2.quantity)")
        print("noodle3:\(noodle3.name)+\(noodle3.quantity)")
        let shoplist = ShoppingList(name: "冰淇淋", quantity: 2)
        print(shoplist.description)
        
        let redCloth = Cloths(color: "")
        if let cloth = redCloth {
            print(cloth.color)
        }
        let blueCloth = Cloths(color: "blue")
        if let cloth = blueCloth {
            print(cloth.color)
        }
        
        let fahrenheitUnit = TemperatureUnit(symbol: "F")
        print(fahrenheitUnit ?? "初始化失败")
        
        let carItem1 = CarItem(name: "吉普", quantity: 2)
        let carItem2 = CarItem(name: "", quantity: 3)
        let carItem3 = CarItem(name: "玛莎拉蒂", quantity: 0)
        print(carItem1?.name ?? "初始化失败")
        print(carItem2?.quantity ?? -1)
        print(carItem3?.name ?? "初始化失败")
        

        let someone = SomeClass()
        print(someone.boardColors.count)
        print(someone.squareInBlcakAtRow(row: 0, column: 1))
        print(someone.squareInBlcakAtRow(row: 0, column: 2))
        // Do any additional setup after loading the view.
    }

    //无实际参数标签的初始化参数
    struct Color {
        let red,green,blue:Double
        init(red:Double,green:Double,blue:Double) {
            self.red = red
            self.green = green
            self.blue = blue
        }
        
        init(white:Double) {
            red = white
            green = white
            blue = white
        }
        
        init(_ black:Double) {
            red = black
            green = black
            blue = black
        }
    }
    
    //可选属性以及常量属性
    class Questions{
        let text:String
        var response:String?
        init(text:String) {
            self.text = text
        }
        func answer()  {
            print(self.response ?? "这个问题还没人回答")
        }
    }
    
    //默认初始化器
    class list {
        var name :String?
        var quantity = 1
        var purchased = false
        var age : Int = 0
    }


    
    //值类型的初始化器委托
    struct Size {
        var width = 0.0,height = 0.0
    }
    struct Point {
        var x = 0.0,y = 0.0
    }
    struct rect {
        var size = Size()
        var origin = Point()
        
        init(){}
        init(origin:Point,size:Size) {
            self.origin = origin
            self.size = size
        }
        init(center:Point,size:Size) {
            let originX = center.x - (size.width/2.0)
            let originY = center.y - (size.height/2.0)
            
            self.init(origin:Point(x: originX, y: originY),size:size)
        }
    }
    
    //初始化器的继承和重写
    class Vehicle {
        var numberOfWheels = 0
        var description:String{
            return "\(numberOfWheels)wheel(s)"
        }
    }
    class Bicyle: Vehicle {
        override init(){
            super.init()
            numberOfWheels = 2
        }
    }
    
    //自动继承
    //子类为所有新增属性提供了默认值,下列两个规则适用:
    //如果子类重写了所有的父类的指定构造器,那么子类会自动集成父类的所有便利构造器
    //如果子类没有任何指定构造器,子类会自动继承所有父类的指定构造器和便利构造器
    class Food {
        var name:String
        init(name:String) {
            self.name = name
        }
        convenience init(){
            self.init(name:"[Unnamed]")
        }
    }
    
    class Noodle: Food {
        var quantity:Int
        init(name:String,quantity:Int) {
            self.quantity = quantity
            super.init(name: name)
        }
        
        override convenience init(name:String){
            self.init(name:name,quantity:1)
        }
    }
    
    class ShoppingList: Noodle {
        var purchased = false
        var description :String{
            var output = "\(quantity)*\(name)"
            output += purchased ? "?":"!"
            return  output
        }
    }
    
    //可失败构造器
    struct Cloths{
        let color :String
        init?(color:String) {
            if color.isEmpty {return nil}
            self.color = color
        }
    }
    
    //枚举的可失败初始化构造器
    enum TemperatureUnit:String {
        case Kelvin,Celsius,Fahrenheit
        init?(symbol:Character) {
            switch symbol {
            case "K":
                self = .Kelvin
            case "C":
                self = .Celsius
            case "F":
                self = .Fahrenheit
            default:
                return nil
            }
        }
    }
    
    //可失败初始化构造器的传递
    class Product{
        let name :String
        init?(name:String) {
            if name.isEmpty{ return nil }
            self.name = name
        }
    }
    
    class CarItem: Product {
        let quantity:Int
        init?(name:String,quantity:Int) {
            if quantity < 1 { return nil }
            self.quantity = quantity
            super.init(name: name)
        }
    }
    
    //重写可失败构造器
    class Documet {
        var name:String?
        init() {}
        init?(name:String) {
            self.name = name
            if name.isEmpty {
                return nil
            }
        }
    }
    
    class AutomaticallyNamedDocument: Documet {
        override init(){
            super.init()
            self.name = "[Untitled]"
        }
        
        override init(name:String){
            var noEmpty = "[Untitled]"
            if !name.isEmpty {
                noEmpty = name
            }
            super.init(name: noEmpty)!
        }
    }
    
    //通过闭包或函数设置属性的默认值
    class SomeClass {
        let boardColors:[Bool] = {
            var arr = [Bool]()
            var isBlack = false
            for i in 1...10{
                for j in 1...10{
                    arr.append(isBlack)
                    isBlack = !isBlack
                }
                isBlack = !isBlack
            }
            return arr
        }()
        func squareInBlcakAtRow(row:Int,column:Int) -> Bool {
            assert(!(row<1 || row>10 || column<1 || column>10), "boardColors越界")
            return boardColors[(row*10)+column]
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
